package kz.pompei.ifacer.visio.def.paint;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;

import javax.swing.JComponent;
import javax.swing.JOptionPane;

import kz.pompei.ifacer.cursor_manager.def.DefRectGeo;
import kz.pompei.ifacer.grot.acts.Act;
import kz.pompei.ifacer.grot.acts.ChangeFieldsAct;
import kz.pompei.ifacer.model.CursorInfo;
import kz.pompei.ifacer.model.CursorKind;
import kz.pompei.ifacer.model.PaintKind;
import kz.pompei.ifacer.model.widgets.WidgetForm;
import kz.pompei.ifacer.util.RichText;
import kz.pompei.ifacer.util.TextSize;
import kz.pompei.ifacer.visio.PaintUtil;

public class PaintWidgetForm {
  
  public void
      paint(Graphics2D g, WidgetForm f, PaintKind paintKind, Point mouse, Rectangle allSpace) {
    if (paintKind != PaintKind.MOVING) {
      g.setColor(new Color(0xF8F8F8));
      g.fillRect(f.x, f.y, f.width, f.height);
    }
    
    RichText title = RichText.parse(f.title);
    TextSize s = title.getSize(g.create());
    
    int captionHeight = s.height();
    
    if (paintKind != PaintKind.MOVING) {
      Rectangle rect = new Rectangle(f.x, f.y, f.width, captionHeight + PaintUtil.BOTTOM_OFFSET);
      if (rect.contains(mouse)) {
        g.setColor(new Color(0xA8A8A8));
      } else {
        g.setColor(new Color(0xD8D8D8));
      }
      g.fillRect(f.x, f.y, f.width, captionHeight + PaintUtil.BOTTOM_OFFSET);
    }
    
    if (paintKind.isOver()) {
      PaintUtil.drawOverRect(g, f.rect());
    } else {
      g.setColor(paintKind == PaintKind.MOVING ? Color.GRAY :Color.BLACK);
      g.drawRect(f.x, f.y, f.width, f.height);
    }
    
    {
      g.setColor(paintKind == PaintKind.MOVING ? Color.GRAY :Color.BLACK);
      g.drawLine(f.x, f.y + captionHeight + PaintUtil.BOTTOM_OFFSET, f.x + f.width, f.y
          + captionHeight + PaintUtil.BOTTOM_OFFSET);
    }
    
    if (f.title != null) {
      Graphics2D g2 = (Graphics2D)g.create();
      g2.clipRect(f.x, f.y, f.width, captionHeight + PaintUtil.BOTTOM_OFFSET);
      g2.setColor(paintKind == PaintKind.MOVING ? Color.GRAY :Color.BLACK);
      g2.drawString(f.title, f.x + PaintUtil.BOTTOM_OFFSET, f.y + captionHeight);
      g2.dispose();
    }
    
    if (paintKind.isSel()) {
      PaintUtil.drawSelectionRect(g, f.x, f.y, f.width, f.height);
    }
  }
  
  public CursorInfo getCursorInfo(Graphics2D g, WidgetForm f, Point mouse) {
    
    //hit border
    {
      CursorKind cursorKind = getBorderCursor(f, mouse);
      if (cursorKind != null) {
        return new CursorInfo(cursorKind, null);
      }
    }
    
    //hit title
    {
      if (inTitle(g, f, mouse)) {
        CursorInfo ret = new CursorInfo(CursorKind.MOVE, "edit");
        return ret;
      }
    }
    
    //hit body
    if (new Rectangle(f.x, f.y, f.width, f.height).contains(mouse)) {
      CursorInfo ret = new CursorInfo(CursorKind.MOVE, null);
      return ret;
    }
    
    //missed
    return null;
  }
  
  private boolean inTitle(Graphics2D g, WidgetForm f, Point mouse) {
    RichText rt = RichText.parse(f.title);
    TextSize size = rt.getSize(g.create());
    int captionHeight = size.height();
    
    return new Rectangle(f.x, f.y, f.width, captionHeight + PaintUtil.BOTTOM_OFFSET)
        .contains(mouse);
  }
  
  private CursorKind getBorderCursor(WidgetForm f, Point point) {
    return DefRectGeo.cursorKindOnBorder(point, f, PaintUtil.BORDER_WIDTH, PaintUtil.CORNER_SIZE);
  }
  
  public Rectangle getWidgetBound(Graphics2D g, WidgetForm f) {
    return f.rect();
  }
  
  public Act
      move(Graphics2D g, WidgetForm original, WidgetForm copy, Point fromPoint, Point toPoint)
          throws Exception {
    
    int dx = toPoint.x - fromPoint.x;
    int dy = toPoint.y - fromPoint.y;
    
    CursorKind borderCursor = getBorderCursor(original, fromPoint);
    
    if (borderCursor == null) borderCursor = CursorKind.MOVE;
    
    return PaintUtil.resize(dx, dy, borderCursor, original, copy);
  }
  
  public Act getDoubleClickAct(Graphics2D g, WidgetForm w, Point mouse, JComponent owner)
      throws Exception {
    
    if (inTitle(g, w, mouse)) {
      String newTitle = JOptionPane.showInputDialog(owner, "Укажите новый заголовок", w.title);
      if (newTitle == null) return null;
      return new ChangeFieldsAct(w.uuid, "title", newTitle);
    }
    
    return null;
  }
}
